home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / ee1091.zip / EE.C next >
Text File  |  1991-10-11  |  22KB  |  1,077 lines

  1. /* ee.c, Yijun Ding, Copyright 1991
  2.  
  3. Compile with TC: tcc ee.c
  4. Compile in unix: gcc ee.c -lcursesX
  5.  
  6. Prototype are listed in the order defined. They are essentially grouped
  7. as cursor move, screen, search, files, misc, mainloop, main.
  8.  
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define EOL '\0'    /* end of line marker */
  14. #define BLK ' '     /* blank */
  15. #define LF  '\n'    /* new line */
  16. #define NLEN  80    /* buffer line for file name */
  17. #define LMAX  10000 /* max line length */
  18. #define XINC  20    /* increament of x */
  19. #define HLP 28
  20.  
  21. #define CHG 0
  22. #define FIL 1   /* fill */
  23. #define OVR 2   /* insert */
  24. #define CAS 3   /* case sensative */
  25. #define TAB 4   /* tab */
  26. #define POS 5   /* show pos */
  27. #define ALT 6   /* meta_key */
  28. #define SHW 7   /* screen */
  29. #define NEW 8   /* new file */
  30. #define EDT 9   /* quit edit */
  31. #define WIN 10  /* window */
  32. #define NTS 11  /* note posted */
  33. #define ALL 12  /* last flag, quit */
  34. char  flag[ALL+1];
  35. char  fsym[]="|foctp~";
  36.  
  37. typedef struct {
  38.   char name[NLEN];
  39.   int  jump;
  40. } MWIN;          /* my window structure */
  41.  
  42. /* order of \n\r is important for curses */
  43. #define HELP_STR "\n\
  44. Usage: ee [-+line(def=1) -ttab(def=2) file(def=Notes)]\n\r\
  45. \n\
  46. A set mark, append file;    B format, right margin;\n\r\
  47. C shell, execute line;      D cursor right, line end;\n\r\
  48. E cursor up, file top;      F forward search, backward search;\n\r\
  49. G goto line, goto column;   H delete before, delete under;\n\r\
  50. I tab, tab insert;          J play macro, record;\n\r\
  51. K block copy, block cut;    L refresh screen, status;\n\r\
  52. M return;                   N toggle insert mode;\n\r\
  53. O toggle mode;              P command prefix;\n\r\
  54. Q command prefix;           R replace, replace all;\n\r\
  55. S cursor left, line begin;  T delete word, del eol;\n\r\
  56. U block paste, block write; V page down, page up;\n\r\
  57. W window, window cancel;    X cursor down, file bottom;\n\r\
  58. Y block delete line;        Z exit, edit other file;\n\r\
  59. \n\
  60. Each letter represent a control key. Each key corresponds one or two\n\r\
  61. editing commands (seperated by comma). Press the control key for the\n\r\
  62. first editing function, or control-P and the control key for the second\n\r\
  63. editing function.\n\n\r\
  64. Press any key to continue ..."
  65.  
  66. #ifdef __TURBOC__
  67. #define AMAX  0xD000  /* main buffer size */
  68. #define BMAX  0x400   /* block size */
  69. #define YTOP  1
  70.  
  71. #define ttopen()    sww=76;swhfull=23
  72. /* #define highvideo() textattr(0x30)
  73. #define lowvideo()  textattr(0x1F)
  74. #define ttclose()   textattr(0x07) */
  75. #define ttclose()   normvideo()
  76.  
  77. /* honest get a key */
  78. int get_key()
  79. {
  80.   static  char k1[]="GHIKMOPQRS";
  81.   static  char k2[]="sEvSDdXVNh";
  82.   int  key;
  83.   char *s;
  84.  
  85.   if((key=getch()) == 0) {
  86.     if((s=strchr(k1, getch())) == 0)
  87.       return HLP;
  88.     if((key = k2[s-k1]) > 'a')
  89.       flag[ALT]++;
  90.     return key&0x1F;
  91.   }
  92.   return key;
  93. }
  94. #else
  95. #include "eeibm.c"
  96. #endif
  97.  
  98. char  bbuf[10];               /* backup file name */
  99. char  sbuf[NLEN], rbuf[NLEN]; /* search buffer, replace buffer */
  100. char  *ae, aa[AMAX];          /* main buffer */
  101. char  bb[BMAX], *mk;          /* block buffer, mark */
  102. unsigned blen;
  103. char  *dp, *ewb;              /* current pos, line */
  104.  
  105. int xtru, ytru;               /* file position */
  106. int ytot;                     /* 0 <= ytru <= ytot */
  107.  
  108. int swhfull;                  /* screen physical height */
  109. int x, sww;                   /* screen position 1 <= x <= sww */
  110. int y, swh;                   /* screen size 0 <= y <= swh */
  111. int y1, y2;                   /* 1st, 2nd line of window */
  112. int tabsize=8;                /* tab size */
  113.  
  114. FILE  *fi, *fo;
  115. MWIN  win, winnext, wincopy;  /* current, next, other windows */
  116.  
  117. void cursor_up(), cursor_down(), cursor_left(), cursor_right();
  118. void show_rest(int len, char *s);
  119. void show_scr(int fr, int to);
  120. void show_sup(int line), show_sdn(int line);
  121. void show_flag(int x, int g);
  122. void show_note(char *prp);
  123. int  show_gets(char *prp, char *buf);
  124. void show_top(), show_help(), show_mode(), show_status();
  125. void file_read();
  126. char *file_ltab(char *s);
  127. int  file_write(FILE *fp, char *s, char *e);
  128. int  file_fout();
  129. void file_save(int f_all, int f_win);
  130. void file_more();
  131. void file_rs(char *s, char *d);
  132. void goto_x(int xx), goto_y(int yy);
  133. void goto_ptr(char *s);
  134. void goto_row(), goto_col();
  135. int  str_cmp(char *s);
  136. char *goto_find(char *s, int  back);
  137. void goto_search(int back);
  138. void goto_replace(int whole);
  139. void window_other(), window_cancel(), window_size();
  140. void block_put(), block_get(), block_mark();
  141. void block_copy(int delete);
  142. void block_paste(), block_write(), block_line();
  143. int  block_fill();
  144. void block_format();
  145. void key_return(), key_deleol(char *s), key_delete();
  146. void key_backspace(), key_delword(int eol);
  147. void key_tab(int tabi);
  148. void key_normal(int key);
  149. void key_shell(int line);
  150. void key_macros(int record);
  151. void main_meta(int key), main_exec(int key);
  152. void main_loop(), main(int argc, char **argv);
  153.  
  154. /* cursor movement ----------------------------------------- */
  155. void cursor_up()
  156. {
  157.   if(ytru == 0) return;
  158.   ytru--;
  159.   while(*--ewb != EOL) ;
  160.   y--;
  161. }
  162.  
  163. void cursor_down()
  164. {
  165.   if(ytru == ytot) return;
  166.   ytru++;
  167.   while(*++ewb != EOL) ;
  168.   y++;
  169. }
  170.  
  171. /* cursor left & right: x, xtru */
  172. void cursor_left()
  173. {
  174.   if(xtru == 1) return;
  175.   xtru--;
  176.   if(--x < 1) {
  177.     x += XINC;
  178.     flag[SHW]++;
  179.   }
  180. }
  181.  
  182. void cursor_right()
  183. {
  184.   if(xtru == LMAX) return;
  185.   xtru++;
  186.   if(++x > sww) {
  187.     x -= XINC;
  188.     flag[SHW]++;
  189.   }
  190. }
  191.  
  192. #define cursor_pageup() {int i; for(i=1; i<swh; ++i) cursor_up();}
  193. #define cursor_pagedown(){int i; for(i=1; i<swh; ++i) cursor_down();}
  194.  
  195. /* dispaly --------------------------------------------------------*/
  196. /* assuming cursor in correct position: show_rest(sww-x,ewb+xtru) */
  197. void show_rest(len, s)
  198. int  len;
  199. char *s;
  200. {
  201.   char save;
  202.   save = s[len];
  203.   s[len] = 0;
  204.   cputs(s);
  205.   s[len] = save;
  206.   clreol();
  207. }
  208.  
  209. /* ewb and y correct */
  210. void show_scr(fr,to)
  211. int fr, to;
  212. {
  213.   char *s=ewb;
  214.   int  len=sww-1, i=fr;
  215.   unsigned xl=xtru-x;
  216.  
  217.   /* calculate s */
  218.   for(; i<y; i++) while(*--s != EOL) ;
  219.   for(; i>y; i--) while(*++s != EOL) ;
  220.  
  221.   /* first line */
  222.   s++;
  223.   do {
  224.     gotoxy(1, fr+y2);
  225.     if(s<ae && strlen(s) > xl) show_rest(len, s+xl);
  226.     else clreol();
  227.     while(*s++) ;
  228.   } while(++fr <= to);
  229. }
  230.  
  231. void show_sup(line)
  232. int line;
  233. {
  234.   gotoxy(1, y2+line);
  235.   delline();
  236.   show_scr(swh, swh);
  237. }
  238.  
  239. void show_sdn(line)
  240. int line;
  241. {
  242.   gotoxy(1, y2+line);
  243.   insline();
  244.   show_scr(line, line);
  245. }
  246.  
  247. void show_flag(x, g)
  248. int x, g;
  249. {
  250.   gotoxy(14+x, y1);
  251.   putch(g? fsym[x]: '.');
  252.   flag[x] = g;
  253. }
  254.  
  255. void show_note(prp)
  256. char *prp;
  257. {
  258.   gotoxy(17+ALT, y1);
  259.   cputs(prp);
  260.   clreol();
  261.   flag[NTS]++;
  262. }
  263.  
  264. int show_gets(prp, buf)
  265. char *prp, *buf;
  266. {
  267.   int key;
  268.   int col=(-1);
  269.   show_note(prp);
  270.   cputs(": ");
  271.   cputs(buf);
  272.   for(;;) {
  273.     key = get_key();
  274.     if(key >= BLK) {
  275.       if(col < 0) {
  276.         col++;
  277.         show_note(prp);
  278.         cputs(": ");
  279.       }
  280.       buf[col++] = key;
  281.     }
  282.     else if(key == 8) {
  283.       if(col < 0) col = strlen(buf);
  284.       if(col == 0) continue;
  285.       col--;
  286.     }
  287.     else break;
  288.     putch(key);
  289.   }
  290.   flag[ALT] = 0;
  291.   if(col > 0) buf[col] = 0;
  292.   return (key == 27 || *buf == 0);
  293. }
  294.  
  295. void show_top()
  296. {
  297.   int i;
  298.   gotoxy(1, y1);
  299.   highvideo();
  300.   cputs(win.name);
  301.   lowvideo();
  302.   clreol();
  303.   for(i=0; i<=ALT; i++)
  304.     show_flag(i, flag[i]);
  305.   flag[NTS]++;
  306. }
  307.  
  308. void show_help()
  309. {
  310.   clrscr();
  311.   cputs(HELP_STR);
  312.   get_key();
  313.   show_top();
  314.   flag[SHW]++;
  315. }
  316.  
  317. void show_mode()
  318. {
  319.   char *d;
  320.   int  k;
  321.   show_note(fsym);
  322.   putch(BLK);
  323.   k = get_key()|0x60;
  324.   if((d=strchr(fsym, k)) != 0) {
  325.     k = d-fsym;
  326.     show_flag(k, !flag[k]);
  327.   }
  328. }
  329.  
  330. void show_status()
  331. {
  332.   char tbuf[80];
  333.   sprintf(tbuf, "line %d/%d, col %d, char %u/%u/%u+%u",
  334.     ytru+1, ytot, xtru, dp-aa, ae-aa, BMAX, AMAX);
  335.   show_note(tbuf);
  336.   sleep(1);
  337. }
  338.  
  339. /* file operation ---*/
  340. void file_read()
  341. {
  342.   int  c;
  343.   char *col;
  344.   ewb = aa;
  345.   ae = mk = col = aa+1;
  346.   xtru = x = 1;
  347.   ytru = y